package babo.cd.physicalexperiment.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lxj.xpopup.core.BottomPopupView;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.Map;

import babo.cd.physicalexperiment.R;
import babo.cd.physicalexperiment.bean.JustDataBean;
import babo.cd.physicalexperiment.data.MyEnventData;
import babo.cd.physicalexperiment.data.RecordData;
import babo.cd.physicalexperiment.data.UserInfo;
import babo.cd.physicalexperiment.utils.OKCallback;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.utils.URLS;
import babo.cd.physicalexperiment.view.RoundAngleImageView;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class AddHistoryDialog extends BottomPopupView {

    Context context;
    EditText ed_ct,ed_wt,ed_jsz;
    TextView tx_sure,tx_cancle;
    RoundAngleImageView iv_cutiao,iv_weitiao;
    private String cturl,wturl;
    private File ctfile,wtfile;
    int flag=0;
    public AddHistoryDialog(@NonNull Context context, String cturl, String wturl, File ctfile,File wtfile) {
        super(context);
        this.context=context;
        this.cturl=cturl;
        this.wturl=wturl;
        this.ctfile=ctfile;
        this.wtfile=wtfile;

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_apphistory;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        tx_sure=(TextView)findViewById(R.id.tx_sure);
        tx_cancle=(TextView) findViewById(R.id.tx_cancle);
        ed_ct=(EditText)findViewById(R.id.ed_ct);
        ed_wt=(EditText)findViewById(R.id.ed_wt);
        ed_jsz=(EditText)findViewById(R.id.ed_jsz);
        iv_cutiao=(RoundAngleImageView)findViewById(R.id.iv_cutiao);
        iv_weitiao=(RoundAngleImageView)findViewById(R.id.iv_weitiao);
        tx_cancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ed_ct.setText("");
                ed_wt.setText("");
                ed_jsz.setText("");
                dismiss();
            }
        });

        if (!TextUtils.isEmpty(cturl)){
            Glide.with(context).load(cturl).into(iv_cutiao);
        }
        if (!TextUtils.isEmpty(wturl)){
            Glide.with(context).load(wturl).into(iv_weitiao);
        }
        setLister();

    }

    private void setLister() {
        tx_sure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(ed_ct.getText().toString().trim())||TextUtils.isEmpty(ed_wt.getText().toString().trim())||TextUtils.isEmpty(ed_jsz.getText().toString().trim())){
                    Out.toast(context,"请填写完整数据！");
                    return;
                }

                post3(URLS.uploadImage,new OKCallback(context){
                    @Override
                    public void Sucess(String str) {
                        super.Sucess(str);
                        try {
                            Gson gson=new Gson();

                            JustDataBean justDataBean=gson.fromJson(str,JustDataBean.class);
                            Out.out("上传回来的图片地址："+justDataBean.getData());
                            flag=flag+1;
                            cturl=justDataBean.getData();
                            if (flag==2){
                                doAddHisotry();
                            }
                        }catch (Exception ex){
                            ex.printStackTrace();
                        }
                    }
                },null,ctfile,null,null,UserInfo.JsessionId);
                post3(URLS.uploadImage,new OKCallback(context){
                    @Override
                    public void Sucess(String str) {
                        super.Sucess(str);
                        try {
                            Gson gson=new Gson();

                            JustDataBean justDataBean=gson.fromJson(str,JustDataBean.class);
                            Out.out("上传回来的图片地址："+justDataBean.getData());
                            flag=flag+1;
                            wturl=justDataBean.getData();
                            if (flag==2){
                                doAddHisotry();
                            }
                        }catch (Exception ex){
                            ex.printStackTrace();
                        }
                    }
                },null,wtfile,null,null,UserInfo.JsessionId);




            }
        });
    }

    private void doAddHisotry() {
        RecordData recordData=new RecordData();
        recordData.setLargeImgUrl(cturl);
        recordData.setLargeValue(ed_ct.getText().toString().trim());
        recordData.setResultValue(ed_ct.getText().toString().trim());
        recordData.setSmallImgUrl(wturl);
        recordData.setSmallValue(ed_wt.getText().toString().trim());
        Gson gson=new Gson();
        String shuju=gson.toJson(recordData);
        EventBus.getDefault().post(MyEnventData.getInstance("addhistory@"+shuju));
        ed_jsz.setText("");
        ed_wt.setText("");
        ed_ct.setText("");
        dismiss();
    }

    public static void post3(String address, okhttp3.Callback callback, Map<String, String> map, File mfile, File mfile2, File mfile3, String token) {

        address= URLS.HTTPHEAD+address;
        OkHttpClient client = new OkHttpClient();
        MultipartBody.Builder builder = new MultipartBody.Builder();
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                builder.addFormDataPart(entry.getKey(), entry.getValue());
            }
        }


        if (mfile != null) {


            String TYPE = "application/octet-stream";
            RequestBody fileBody = RequestBody.create(MediaType.parse(TYPE), mfile);
            RequestBody requestBody = builder
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("image ", mfile.getName(), fileBody)
                    .build();
            Request request = new Request.Builder()
                    .url(address)
                    .post(requestBody)
                    .addHeader("Cookie", "JSESSIONID="+ UserInfo.JsessionId)
                    .build();
            client.newCall(request).enqueue(callback);
            Out.out("开始网络请求没111=="+address);

        } else {
//            Log.d(TAG, "post3: 文件不存在");
//            RequestBody requestBody = builder
//                    .setType(MultipartBody.FORM)
//                    .build();
//            Request request = new Request.Builder()
//                    .url(address)
//                    .post(requestBody)
//                    .addHeader("Cookie", "JSESSIONID="+ UserInfo.JsessionId)
//                    .build();
//            client.newCall(request).enqueue(callback);
            Out.out("开始网络请求没：000---"+address);
        }

    }
}
